import NotFound from "@/views/NotFound.vue";
import "nprogress/nprogress.css";
import { start, done, configure } from "nprogress";
configure({
	trickleSpeed: 100,
});

function delay(duration) {
	return new Promise((resolve) => {
		setTimeout(() => {
			resolve();
		}, duration);
	});
}

//该函数只在第一次加载页面执行，后面是直接使用该函数的返回值
function getPageComponent(pageCompResolver) {
	return async () => {
		// console.log("组件开始加载");
		start(); //页面顶部加载效果
		//判断当前是否是开发环境
		if (process.env.NODE_ENV === "development") {
			await delay(2000);
		}
		const comp = await pageCompResolver();
		// console.log("组件加载完成");
		done();
		return comp;
	};
}

export default [
	{
		name: "Home",
		path: "/",
		component: getPageComponent(() =>
			import(/*webpackChunkName:"home"*/ "@/views/Home")
		),
		meta: {
			title: "首页",
		},
	},
	{
		name: "Blog",
		path: "/blog",
		component: getPageComponent(() =>
			import(/*webpackChunkName:"blog"*/ "@/views/Blog")
		),
		meta: {
			title: "文章详情",
		},
	},
	{
		name: "CategoryBlog",
		path: "/blog/cate/:categoryId",
		component: getPageComponent(() =>
			import(/*webpackChunkName:"blog"*/ "@/views/Blog")
		),
	}, //动态路由
	{
		name: "BlogDetail",
		path: "/blog/detail/:blogId",
		component: getPageComponent(() =>
			import(/*webpackChunkName:"detail"*/ "@/views/Blog/Detail")
		),
	}, //动态路由
	{
		name: "About",
		path: "/about",
		component: getPageComponent(() =>
			import(/*webpackChunkName:"about"*/ "@/views/About")
		),
		meta: {
			title: "关于我",
		},
	},
	{
		name: "Project",
		path: "/project",
		component: getPageComponent(() =>
			import(/*webpackChunkName:"project"*/ "@/views/Project")
		),
		meta: {
			title: "项目&效果",
		},
	},
	{
		name: "Message",
		path: "/message",
		component: getPageComponent(() =>
			import(/*webpackChunkName:"message"*/ "@/views/Message")
		),
		meta: {
			title: "留言板",
		},
	},
	{
		name: "NotFound",
		path: "*",
		component: NotFound,
	},
];
